package com.lvtn.adsonandroid;

import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class AccessPrivateInforActivity extends Activity {
//	public final static String EDIT_MESSAGE = "com.example.android.MESSAGE";
	//public final static String NAME = "com.example.android.MESSAGE";
	//public final static String EMAIL = "com.example.android.MESSAGE";
	//public final static String PHONE = "com.example.android.MESSAGE";
	//public final static String ADDRESS = "com.example.android.MESSAGE";
	private String mName;
	private String mPhone;
	private String mAddress;
	private String mEmail;
	private String avarta;
	private GetUserDetail mAuthTask = null;
	private TextView name;
	private TextView email;
	private TextView phone;
	private TextView address;
	private TextView created_at;
	private ImageView image;
	private ImageLoader imageloader;
	//private Button edit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_access_private_infor);
		image = (ImageView) findViewById(R.id.imgavat);
		
		Intent intent = getIntent();
		Log.d("test","van on 5");
		mEmail = intent.getStringExtra(ManageActivity.EXTRA_MESSAGE);
		Log.d("User Detail", mEmail);
		mAuthTask = new GetUserDetail();
		mAuthTask.execute((Void) null);
		
		findViewById(R.id.edit).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent edit = new Intent(getApplicationContext(),EditUserDetailActivity.class);
						Bundle bundle = new Bundle();
						
						bundle.putString("NAME",mName);
						//Log.d("tennnn",NAME);
						bundle.putString("EMAIL",mEmail );
						bundle.putString("PHONE",mPhone );
						//Log.d("phoneeeeee",PHONE);
						bundle.putString("ADDRESS",mAddress );
						bundle.putString("AVARTA",avarta );
						//Log.d("addressss",ADDRESS);
						edit.putExtras(bundle);
						startActivity(edit);
					}
				});
		findViewById(R.id.comment).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Intent get = new Intent(getApplicationContext(),GetYourCommentActivity.class);
						get.putExtra("email", mEmail);
						startActivity(get);
					}
				});
	}
	//-----------------------------------------------------------------------------
	
	public class GetUserDetail extends AsyncTask<Void, Void, JSONObject> {
		@Override
		protected JSONObject doInBackground(Void... params) {
			// TODO: attempt authentication against a network service.
            UserFunctions user=new UserFunctions();
            JSONObject json1=user.UserDetail(mEmail);//(mEmail, mPassword);
            Log.d("json",json1.toString());
			try {
				if(json1.getString("success")!= null){
					// receive response
					String result=json1.getString("success");
					if(Integer.parseInt(result)==1){
						
						return json1;
					}
					else return null;
				}
				// Simulate network access.
				//Thread.sleep(2000);
			} catch (JSONException e) {
				return null;
			}

			return json1;
		}

		@Override
		protected void onPostExecute(final JSONObject object) {
			//mAuthTask = null;
			JSONObject user;
			try {
				
				user = object.getJSONObject("user");
				//JSONObject user = userArray.getJSONObject(0);
				name = (TextView) findViewById(R.id.user_name);
				email = (TextView) findViewById(R.id.user_email);
				phone = (TextView) findViewById(R.id.user_phone);
				address = (TextView) findViewById(R.id.user_address);
				created_at = (TextView) findViewById(R.id.user_created_at);
				name.setText(user.getString("name"));
				email.setText(user.getString("email"));
				phone.setText(user.getString("phonenumber"));
				address.setText(user.getString("address"));
				created_at.setText(user.getString("created_at"));
				mEmail = user.getString("email");
				mName = user.getString("name");
				mPhone = user.getString("phonenumber");
				mAddress = user.getString("address");
				avarta = "http://10.0.2.2/android/avarta/"+ user.getString("avarta");
				Log.d("link",avarta);
				imageloader = new ImageLoader(getApplicationContext());
				imageloader.DisplayImage(avarta, image);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

		@Override
		protected void onCancelled() {
			mAuthTask = null;
		//	showProgress(false);
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.access_private_infor, menu);
		return true;
	}

}
